package com.setup.trac.controller.common.loaders;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.setup.trac.controller.ControllerUtils;
import com.setup.trac.pojo.Progetti;
import com.setup.trac.pojo.wrappers.UtentiView;
import com.setup.trac.service.SearchService;

public class LoadProgetti implements HttpObjectsLoader {

	private SearchService searchService;

	public void load(HttpServletRequest request, HttpServletResponse response) {
		if(request.getSession(false)==null){
			try {
				request.getRequestDispatcher("login.do").forward(request, response);
			} catch (ServletException e) {
			} catch (IOException e) {
			}
			return;
		}
		Progetti[] progettis = (Progetti[]) request.getSession(false)
				.getAttribute("progetti");
		if (progettis == null) {
			progettis = getSearchService()
					.find(new Progetti[0], Progetti.class);
			request.getSession(false).setAttribute("progetti", progettis);
		}
		UtentiView uv = ControllerUtils.getUtenteLoggato(request);
		progettis = (Progetti[]) request.getSession(false).getAttribute(
				"progettiUtente");
		if (progettis == null) {
			progettis = (Progetti[]) uv.getUtenti().getProgettis().toArray(new Progetti[0]);
			request.getSession(false).setAttribute("progettiUtente", progettis); 
		}
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

}
